package ywpt.web.action.config;

import java.io.File;
import java.io.InputStream;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import ywpt.core.web.BaseAction;
import ywpt.model.ProjectInfo;
import ywpt.model.ProjectScans;
import ywpt.service.config.ProjectInfoService;
import ywpt.service.config.ProjectScansService;

public class ProjectScansAction extends BaseAction implements ModelDriven<ProjectScans>,Preparable{

	/**
	 * 项目附件Action
	 */
	private static final long serialVersionUID = -2749631841643908274L;
	private static final String URL="/projectScans/";
	private Long id;
	private ProjectScans projectScans;
	private ProjectScansService projectScansService;
	private ProjectInfoService projectInfoService;
	private ProjectInfo projectInfo;
	
	private Long projectId;
	private Long[] scanTypeId;
	
	// 文件的上传
	private File[] upload;// 实际上传文件
	private String[] uploadFileName; // 上传文件名
	private String[] uploadContentType; // 文件的内容类型	

	//文件的下载
	private String fileName;
	private String fileRealName;
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public ProjectScans getProjectScans() {
		return projectScans;
	}

	public void setProjectScans(ProjectScans projectScans) {
		this.projectScans = projectScans;
	}

	public ProjectScansService getProjectScansService() {
		return projectScansService;
	}

	public void setProjectScansService(ProjectScansService projectScansService) {
		this.projectScansService = projectScansService;
	}

	public Long getProjectId() {
		return projectId;
	}

	public void setProjectId(Long projectId) {
		this.projectId = projectId;
	}
	public File[] getUpload() {
		return upload;
	}

	public void setUpload(File[] upload) {
		this.upload = upload;
	}

	public String[] getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String[] uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String[] getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String[] uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getFileRealName() {
		return fileRealName;
	}

	public void setFileRealName(String fileRealName) {
		this.fileRealName = fileRealName;
	}
	public Long[] getScanTypeId() {
		return scanTypeId;
	}

	public void setScanTypeId(Long[] scanTypeId) {
		this.scanTypeId = scanTypeId;
	}
	/*-----------------------------------------------------------------------------*/
	@Override
	public ProjectScans getModel() {
		return this.projectScans;
	}

	@Override
	public void prepare() throws Exception {
		if(id!=null){
			this.projectScans = this.projectScansService.getProjectScansById(id);
		}else{
			this.projectScans = new ProjectScans();
		}
	}

   /*===================================================================================*/
	
	/**
	 *上传项目的附件 
	 */
	public String scanUpload() throws Exception {
		try {
			projectInfo = this.projectInfoService.getProjectInfoById(projectId);
			if (projectInfo.getResponser()!=null&&projectInfo.getResponser().getId().equals(this.getCurrentUser().getPeople().getId())) {
				String url = ServletActionContext.getServletContext().getRealPath(URL);
				File file_date = new File(url);
				if(!file_date.exists()){
					file_date.mkdirs();
				}	
			for (int i = 0; i < upload.length; i++) {
				if(upload[i].exists()){
					String type = uploadContentType[i]; // 文件的内容类型
					String fileName = uploadFileName[i];
					String realName = UUID.randomUUID().toString() + getExt(fileName);
					File target = new File(url, realName);
					FileUtils.copyFile(upload[i], target);
					
					ProjectScans ps = new  ProjectScans();
					ps.setFileName(fileName);
					ps.setContentType(type);
					ps.setRealName(realName);
					this.projectScansService.saveProjectScans(ps,projectId,scanTypeId[i]);
				}
			}
			this.addActionMessage("附件已保存!");
			} else {
				this.addActionMessage("你不是该项目的负责人，不能进行信息的更新！");
			}
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			this.addActionError("保存错误:" + e.getMessage());
			return SUCCESS;
		}
	}	
	
	public static String getExt(String fileName) {
		return fileName.substring(fileName.lastIndexOf("."));
	}	
	
	
	/**
	 *下載公司的附件 
	 */
	public String scanDownload(){
		return SUCCESS;
	}
	
	public void setFileName()
	{
		// 得到请求下载的文件名
		projectScans = this.projectScansService.getProjectScansById(id);
		this.fileName = projectScans.getRealName(); //uuid的名字
		this.fileRealName = projectScans.getFileName();//文件上传的时候的原来的名字
	}

	/*
	 * @getFileName 此方法对应的是struts.xml文件中的： 
	 * <param name="contentDisposition">attachment;filename="${fileName}"</param>
	 * 这个属性设置的是下载工具下载文件时显示的文件名， 要想正确的显示中文文件名，我们需要对fileName再次编码
	 * 否则中文名文件将出现乱码，或无法下载的情况
	 */
	public String getDownloadFileName() throws Exception
	{
		return new String(fileRealName.getBytes( ),"ISO-8859-1");
	}

	/*
	 * @getDownloadFile 此方法对应的是struts.xml文件中的： <param
	 * name="inputName">downloadFile</param> 返回下载文件的流，可以参看struts2的源码
	 */
	public InputStream getDownloadFile()
	{

		this.setFileName();
		return ServletActionContext.getServletContext().getResourceAsStream(URL + fileName);
	}

	public String delete() {
		projectInfo = this.projectScansService.getProjectScansById(id).getProject();
		if (projectInfo.getResponser()!=null&&projectInfo.getResponser().getId().equals(this.getCurrentUser().getPeople().getId())) {
			if (id!=null) {
				projectScansService.removeProjectScansById(id);
			}
		} else {
			this.addActionMessage("你不是该项目的负责人，不能进行信息的更新！");
		}
		
		return SUCCESS;
	}

	public ProjectInfoService getProjectInfoService() {
		return projectInfoService;
	}

	public void setProjectInfoService(ProjectInfoService projectInfoService) {
		this.projectInfoService = projectInfoService;
	}

	public ProjectInfo getProjectInfo() {
		return projectInfo;
	}

	public void setProjectInfo(ProjectInfo projectInfo) {
		this.projectInfo = projectInfo;
	}

	
}
